package com.yanqu.road.server.cmd.activity.tonbingchannel.channel;

import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingConfig;
import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingTitleConfig;
import com.yanqu.road.entity.enums.activity.tonbingchannel.eCTonBingPosition;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tonbingchannel.ChannelTonBingModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;
import java.util.Map;


@Cmd(code = GameProtocol.S_CHANNEL_CTON_BING_CITY_APPOINT_POSITION, desc = "")
public class TonBingAppointPotionCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        ChannelTonBingProto.ChannelTonBingCityWinerAppointRespMsg reqMsg = ChannelTonBingProto.ChannelTonBingCityWinerAppointRespMsg.parseFrom(packet.getBytes());
        long userId = packet.getPlayerId();
        if(reqMsg.getSendReward()){
            ChannelTonBingConfig config = ChannelTonBingMgr.getConfig();
            Map<Integer,Map<Integer, ChannelTonBingTitleConfig>> configMap = config.getTitleConfigMap();
            for(ChannelTonBingProto.CTonBingPositionMsg posMsg : reqMsg.getPositionList()){
                ChannelTonBingTitleConfig titleConfig = configMap.get(posMsg.getType()).get(posMsg.getPositionId());
                if(!titleConfig.getRewards().equals("0") && !StringUtils.isNullOrEmpty(titleConfig.getRewards())){
                    UserInfo userInfo = UserMgr.getUserInfo(userId);
                    String activityName = ActivityMgr.getActivityName(config.getActivityInfo(), userInfo.getLanguage());
                    String positionName = ServerLanguageMgr.getContent(titleConfig.getName(), userInfo.getLanguage());
                    String title,content;
                    if(titleConfig.getId() == eCTonBingPosition.Master.getValue()){
                        title = ServerLanguageMgr.getContent(config.getMailFirstTitleReward(), userInfo.getLanguage());
                        content = StringUtils.format(ServerLanguageMgr.getContent(config.getMailFirstContentReward(), userInfo.getLanguage()),
                                activityName, positionName);
                    }else{
                        title = ServerLanguageMgr.getContent(config.getMailWinTitleReward(), userInfo.getLanguage());
                        content = StringUtils.format(ServerLanguageMgr.getContent(config.getMailWinContentReward(), userInfo.getLanguage()),
                                activityName, reqMsg.getWinerName(),positionName);
                    }
                    MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), titleConfig.getRewards(), content, title, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
                }
            }
        }
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if(player != null){
            for(ChannelTonBingProto.CTonBingPositionMsg posMsg : reqMsg.getPositionList()){
                player.getModule(ChannelTonBingModule.class).winnerAppoint(posMsg.getType(),posMsg.getPositionId());
                if(reqMsg.getSendReward()){
                    player.getModule(ChannelTonBingModule.class).syncUserData();
                }
            }

        }
    }

}
